 

package com.zdkj.subway.stm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.stm.dto.in.SwStTicketInformationCheckDTO;
import com.zdkj.subway.stm.entity.SwStTicketInformationCheck;
import com.zdkj.subway.stm.fegin.SystemUserService;
import com.zdkj.subway.stm.service.SwStTicketInformationCheckService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 票卡信息查询
 *
 * @author cuilei
 * @date 2020-05-13 15:39:48
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstticketinformationcheck" )
@Api(value = "swstticketinformationcheck", tags = "票卡信息查询管理")
public class SwStTicketInformationCheckController {

    private final  SwStTicketInformationCheckService swStTicketInformationCheckService;
    private final SystemUserService systemUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swStTicketInformationCheck 票卡信息查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStTicketInformationCheckPage(Page page, SwStTicketInformationCheck swStTicketInformationCheck) {
        return R.ok(swStTicketInformationCheckService.page(page, Wrappers.query(swStTicketInformationCheck)));
    }


    /**
     * 通过id查询票卡信息查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStTicketInformationCheckService.getById(id));
    }

    /**
     * 新增票卡信息查询
     * @param swStTicketInformationCheck 票卡信息查询
     * @return R
     */
    @ApiOperation(value = "新增票卡信息查询", notes = "新增票卡信息查询")
    @SysLog("新增票卡信息查询" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_sw_st_ticket_information_check_add')" )
    public R save(@RequestBody SwStTicketInformationCheckDTO swStTicketInformationCheck) {
        List<SwStTicketInformationCheck> saveList = new ArrayList<>();
        for(String cardOrder : swStTicketInformationCheck.getCardOrderList()) {
            SwStTicketInformationCheck defaultObj = ObjectUtils.getDefaultObj(SwStTicketInformationCheck.class);
            defaultObj.setCardOrder(cardOrder);
            BeanUtils.copyProperties(swStTicketInformationCheck, defaultObj);
            saveList.add(defaultObj);
        }
        return R.ok(swStTicketInformationCheckService.saveBatch(saveList));
    }

    /**
     * 修改票卡信息查询
     * @param swStTicketInformationCheck 票卡信息查询
     * @return R
     */
    @ApiOperation(value = "修改票卡信息查询", notes = "修改票卡信息查询")
    @SysLog("修改票卡信息查询" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_sw_st_ticket_information_check_edit')" )
    public R updateById(@RequestBody SwStTicketInformationCheck swStTicketInformationCheck) {
        return R.ok(swStTicketInformationCheckService.updateById(swStTicketInformationCheck));
    }

    /**
     * 通过id删除票卡信息查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除票卡信息查询", notes = "通过id删除票卡信息查询")
    @SysLog("通过id删除票卡信息查询" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stm_sw_st_ticket_information_check_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStTicketInformationCheckService.removeById(id));
    }

    /**
     * 提交票卡信息查询到线网
     * @param ids
     * @return
     */
    @ApiOperation(value = "提交票卡信息查询到线网", notes = "提交票卡信息查询到线网")
    @SysLog("提交票卡信息查询到线网")
    @PostMapping("/submit")
    @PreAuthorize("@pms.hasPermission('stm_sw_st_ticket_information_check_submit')")
    public R submit(@RequestBody List<Integer> ids) {
        List<SwStTicketInformationCheck> submitList = new ArrayList<>();
        SysUser sysUser = systemUserService.info().getData().getSysUser();
        for(Integer id : ids) {
            SwStTicketInformationCheck swStTicketInformationCheck = swStTicketInformationCheckService.getById(id);
            if(swStTicketInformationCheck.getStationDataStatus() == 0 && swStTicketInformationCheck.getLineDataStatus() == 0) {
                swStTicketInformationCheck.setStationDataStatus(1);
                swStTicketInformationCheck.setLineDataStatus(1);
                swStTicketInformationCheck.setSubmitLineCode(sysUser.getLineCode());
                swStTicketInformationCheck.setSubmitStationCode(sysUser.getStationCode());
            }
            submitList.add(swStTicketInformationCheck);
        }
        return R.ok(swStTicketInformationCheckService.updateBatchById(submitList));
    }
}
